﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.Model.DI;
using Model.EntityModel.SysModel;
using Repository.IRepository.ISysRepository;

namespace Repository.Repository.SysRepository
{
    [AppService(ServiceType = typeof(ILocationRepository), ServiceLifetime = LifeTime.Transient)]
    public class LocationRepository : SqlSugarBaseRepository<LocationMaster>, ILocationRepository
    {
        public async Task<List<LocationMaster>> GetLocationsByAreaAsync(string area)
        {
            return await _db.Queryable<LocationMaster>()
                .Where(l => l.Area == area)
                .ToListAsync();
        }
  
        public async Task<bool> UpdateLocationAreaAsync(string locationId, string newArea)
        {
            return await _db.Updateable<LocationMaster>()
                .SetColumns(l => new LocationMaster
                {
                    Area = newArea
                })
                .Where(l => l.Id == locationId)
                .ExecuteCommandAsync() > 0;
        }

        public async Task<List<LocationMaster>> SearchLocationsAsync(string keyword)
        {
            return await _db.Queryable<LocationMaster>()
                .Where(l => l.Name.Contains(keyword) || l.Area.Contains(keyword) || l.Descr.Contains(keyword))
                .ToListAsync();
        }
    }
}
